#ifndef _IPC_DB_H_
#define _IPC_DB_H_

#include "sqlite3.h"

#define SQL_CMD_LEN 1024
#define DATABASE_NAME "./ipcam.db"

// cannot include sign '.',or the first word would be taken for database name
#define IPC_TABLE_NAME "list" 
#define HISTORY_TABLE_NAME "histories"

#define CREATE_IPC_TABLE_FMT \
	"create table %s(id INTEGER PRIMARY KEY autoincrement,dev_id varchar(9),version varchar(10),MAC varchar(32),web_port int,video_port int,dhcp varchar(5), dev_ip varchar(16),subnet varchar(16),gateway varchar(16), dns_ip varchar(16), net_card int default 1);"

#define CREATE_HIST_TABLE_FMT \
	"create table %s(id INTEGER PRIMARY KEY autoincrement,dev_id varchar(9),version varchar(10),MAC varchar(32),web_port int,video_port int,dhcp varchar(5), dev_ip varchar(16),subnet varchar(16),gateway varchar(16), dns_ip varchar(16), net_card int default 1, searchdate datetime default(datetime('now','localtime')));"

// string must be put in single qutoes internal.
#define INSERT_IPC_TABLE_FMT \
	"insert into %s values(NULL,'%s','%s','%s',%d,%d,'%s','%s','%s','%s','%s',%d);" 

#define INSERT_HIST_TABLE_FMT \
	"insert into %s(dev_id,version,MAC,web_port,video_port,dhcp,dev_ip,subnet,gateway,dns_ip,net_card) values('%s','%s','%s',%d,%d,'%s','%s','%s','%s','%s',%d);" 

#define SELECT_TABLE_ALL_FMT \
	"select * from %s;" 

#define DELETE_TABLE_FMT \
	"drop table %s;"

#define TRUNCATE_TABLE_FMT \
	"truncate table %s;" // only delete all data in table

// database interfaces 
int db_init(const char *dbname,const char *table);
int db_insert_ipc(const char *table, const void *data, unsigned int type);
int db_insert_hist(const char *table, const void *data, unsigned int type);
void db_exit(void);

#endif

